package pl.edu.mimuw.irs.rcp.controller.editor.workflow.policy;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.NonResizableEditPolicy;
import org.eclipse.gef.editpolicies.XYLayoutEditPolicy;
import org.eclipse.gef.requests.CreateRequest;

import pl.edu.mimuw.irs.rcp.controller.editor.workflow.command.NodeMoveCommand;
import pl.edu.mimuw.irs.rcp.controller.editor.workflow.part.INodeEditPart;

/**
 * @author Jakub Rauch
 * Created on: 2009-05-17
 */
public class WorkflowXYLayoutEditPolicy extends XYLayoutEditPolicy {

	// *** Attributes

	// *** Methods
	
	@Override
	protected Command createChangeConstraintCommand(EditPart child, Object constraint) {
		if (!(child instanceof INodeEditPart))
			return null;
		if (!(constraint instanceof Rectangle))
			return null;

		INodeEditPart nodePart = (INodeEditPart) child;
		IFigure figure = nodePart.getFigure();
		Rectangle oldBounds = figure.getBounds();
		Rectangle newBounds = (Rectangle) constraint;

		if (oldBounds.width != newBounds.width && newBounds.width != -1)
			return null;
		if (oldBounds.height != newBounds.height && newBounds.height != -1)
			return null;

		NodeMoveCommand command = new NodeMoveCommand(nodePart, oldBounds.getCopy(), newBounds.getCopy());
		return command;
	}

	@Override
	protected Command getCreateCommand(CreateRequest arg0) {
		// XXX Auto-generated method stub
		return null;
	}
	
	@Override
	protected EditPolicy createChildEditPolicy(EditPart child) {
		return new NonResizableEditPolicy();
	}

}
